package kgc.crm.util;

import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import kgc.crm.util.Constant;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtil {

    private static SimpleDateFormat sdf;
    /**
     *
     * @param timeType 0 = 当前天，1 = 本周，2 = 本月，3 = 本季度，4 = 本年度
     * @return
     */
    public static String newDateByTimeType(Integer timeType){
        String startTime = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        switch (timeType){
            case 0:
                startTime = format(new Date(),Constant.DATE_TIME_DAY);
                break;
            case 1:
                calendar.set(Calendar.DAY_OF_WEEK, 2);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                startTime = format(calendar.getTime(),Constant.DATE_TIME_DAY);
                break;
            case 2:
                String format = format(new Date(), Constant.DATE_TIME_MOUTH);
                startTime = format + "-01";
                break;
            case 3:
                startTime = format(getCurrentQuarterStartTime(), Constant.DATE_TIME_MOUTH)+"-01";
                break;
            case 4:
                startTime = format(calendar.getTime(),Constant.DATE_TIME_YEAR)+"-01-01";
                break;
        }
        return startTime + " 00:00:01";
    }

    public static String format(Date date,String patt){
        sdf = new SimpleDateFormat(patt);
        return sdf.format(date);
    }

    /**
     * @Description 根据当前日期获取本季度第一天
     * @param
     * @Throws
     * @Return java.util.Date
     * @Author 徐健
     **/
    public static Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = null;
        try {
            if (currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth <= 9)
                c.set(Calendar.MONTH, 6);
            else if (currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * @Description 根据当前日期获取本季度最后一天
     * @param
     * @Throws
     * @Return java.util.Date
     * @Author 徐健
     **/
    public static Date getCurrentQuarterEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getCurrentQuarterStartTime());
        cal.add(Calendar.MONTH, 2);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    /**
     * 获取季度开始天和结束天
     * @param year 年份
     * @param quarter 第几季度
     * @param flag true = 开始天 false = 结束天
     * @return
     */
    public static Date getStartOrEndDayOfQuarter(int year, int quarter,boolean flag) {
        int month = 0;
        if(flag)
            //使用公式为： 当前季度 - 1 * 3 可以算出第N季度的开始月份 例如  （2 - 1） * 3 = 3  cal.set 是从0开始 所以3 = 4月
            month = (quarter - 1) * 3;
        else
            //使用公式为： 当前季度 * 3 - 1可以算出第N季度的结束月份 例如  2 * 3 - 1 = 5 cal.set 是从0开始 所以5 = 6月
            month = quarter * 3 - 1;
        // 根据月获取开始时间
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        if(flag)
            cal.set(Calendar.DAY_OF_MONTH, 1);
        else
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        return cal.getTime();
    }
    //获取上一周的每天日期
    public static Map<String, Date> getLastWeek() {        // TODO Auto-generated method stub
        Map<String, Date> map = new HashMap<String, Date>();
        Calendar cal = Calendar.getInstance();
        int n = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (n == 0) {
            n = 7;
        }
        cal.add(Calendar.DATE, -(7 + (n - 1)));// 上周一的日期
        Date monday = cal.getTime();
        map.put("monday", monday);
        cal.add(Calendar.DATE, 1);
        Date tuesday = cal.getTime();
        map.put("tuesday", tuesday);

        cal.add(Calendar.DATE, 1);
        Date wednesday = cal.getTime();
        map.put("wednesday", wednesday);

        cal.add(Calendar.DATE, 1);
        Date thursday = cal.getTime();
        map.put("thursday", thursday);

        cal.add(Calendar.DATE, 1);
        Date friday = cal.getTime();
        map.put("friday", friday);

        cal.add(Calendar.DATE, 1);
        Date saturday = cal.getTime();
        map.put("saturday", saturday);

        cal.add(Calendar.DATE, 1);
        Date sunday = cal.getTime();
        map.put("sunday", sunday);
        return map;
    }

    public static void main(String[] args) {


    }

//    public static void main(String[] args) {
//        System.out.println("开始时间:"+format(getStartOrEndDayOfQuarter(2022, 2,true), Constant.DATE_TIME_YYYY_MM_DD_HH_MM_SS));
//        System.out.println("结束时间:"+format(getStartOrEndDayOfQuarter(2022, 2,false),Constant.DATE_TIME_YYYY_MM_DD_HH_MM_SS));
//    }
}